//
//  HPReleaseTextViewController.m
//  fruitsDealers
//
//  Created by sgone on 16/4/25.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "HPReleaseTextViewController.h"
#import "HPReleaseTextScrollView.h"
//#import "HPHttpService.h"
#import "ACTextView.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
#import "UIView+Helper.h"
#import "Toast+UIView.h"

@interface HPReleaseTextViewController () <UITextViewDelegate,UIScrollViewDelegate> {
    
//    HPHttpService *service;
}

@property (nonatomic, strong)UIButton *backButton;
@property (nonatomic, strong)UIButton *sendButton;

@property (nonatomic, strong) HPReleaseTextScrollView *scrollView;
@property (nonatomic, strong)AFHTTPSessionManager *manager;

@end

@implementation HPReleaseTextViewController



- (AFHTTPSessionManager *)manager {
    
    if (_manager == nil) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    self.titleLabel.text = @"水果圈";


    self.view.backgroundColor = FDBack_COLOR;
//    service = [[HPHttpService alloc] init];
    
    [self setupTextView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNavigationBarView];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideRightButton" object:nil userInfo:@{@"state":@"0"}];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.backButton removeFromSuperview];
    [self.sendButton removeFromSuperview];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideRightButton" object:nil userInfo:@{@"state":@"1"}];
    
}

- (void)setNavigationBarView {
    
    CGFloat width = 35 / FDLaOt;
    self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(16, 20 + (44 - width) / 2, width, width)];
    [self.backButton setTitle:@"取消" forState:UIControlStateNormal];
    self.backButton.titleLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    [self.backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.backButton];
    
    self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(ViewWidth - 16 - width, 20 + (44 - width) / 2, width, width)];
    [self.sendButton setTitle:@"发送" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    [self.sendButton addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.sendButton];
}

/**
 *  @author jing
 *
 *  @brief  添加输入控件
 */
- (void)setupTextView
{
    
    self.scrollView = [[HPReleaseTextScrollView alloc] initWithFrame:CGRectMake(0, 64, ViewWidth, ViewHeight - 64)];
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.pagingEnabled = NO;
    self.scrollView.contentSize = CGSizeMake(ViewWidth, ViewHeight);
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.maxLength = 700;
    [self.view addSubview:self.scrollView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoardTap)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    [self.scrollView addGestureRecognizer:tapGesture];
    
    [self.scrollView.contentTextView becomeFirstResponder];
}

- (void)hideKeyBoardTap {
    
    [self.view endEditing:YES];
}

- (void)backClick {
    
  
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendClick {
    
    if (self.scrollView.contentTextView.text.length == 0) {
        
        [self.view makeToast:@"请输入发布内容"];
        
        return;
    }
    
    __weak HPReleaseTextViewController *weakSelf = self;
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    NSString *url = [NSString stringWithFormat:@"%@add_fruit_frend_list",SERVER_ADDRESS3];
    
    NSDictionary *pramas = @{@"tel" : self.dataHandler.phone ,@"uid" : [self.dataHandler.userDic objectForKey:@"wId"] , @"body" : self.scrollView.contentTextView.text , @"imglist" : @"" , @"lat" : @"" , @"lng" : @"", @"user_type" : @"3"};
    
    
    
    [self.manager POST:url parameters:pramas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            UIAlertView *alter  = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            [self.navigationController popViewControllerAnimated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshShareListMethod" object:nil];
            
        }
        else
        {
            
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            [self.view makeToast:str];
            
            
            
            
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self.view makeToast:@"请检查网络!"];
        
        
        
    }];
    
    
    /*
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
     
        [service setShareMessageByBody:self.scrollView.contentTextView.text imglist:[NSNull null] lat:@"" lng:@"" user_type:@"2" success:^(NSString *resultStr) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [MBProgressHUD hideAllHUDsForView:weakSelf.scrollView animated:YES];
                
                weakSelf.rightBut.enabled = YES;
                if ([resultStr isEqualToString:@"success"]) {
                    
                    [MBProgressHUD showSuccess:@"发送成功"];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshShareListMethod" object:nil];
                }
            });
        } error:^(NSString *errorInfo) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [MBProgressHUD hideAllHUDsForView:weakSelf.scrollView animated:YES];
                weakSelf.rightBut.enabled = YES;
                [OtherLogin otherUserLoginTheSameUsername:errorInfo navigationController:weakSelf.navigationController];
            });
        }];
    });
     */
}

@end
